package jp.co.itfrontier.mashup.command {
	import jp.co.itfrontier.mashup.event.IEvent;
	import jp.co.itfrontier.mashup.core.ModelLocater;
	import jp.co.itfrontier.mashup.vo.BookVO;
	import mx.logging.Log;
	import flash.net.getClassByAlias;
	import jp.co.itfrontier.mashup.core.Logger;
	import flash.net.URLVariables;
	import flash.net.URLRequest;
	import flash.net.URLLoader;
	import flash.events.Event;
	import mx.collections.XMLListCollection;
	import jp.co.itfrontier.mashup.core.EventController;
	import jp.co.itfrontier.mashup.utils.CrossDomainUtil;
	import jp.co.itfrontier.mashup.event.ChangeModelEvent;
	import mx.controls.sliderClasses.SliderLabel;
	import mx.managers.PopUpManager;
	import mx.core.Application;
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.display.SpreadMethod;

	public class SearchCommand implements ICommand {

		// kizasi関連語取得最大数
		private const KIZASI_RELATION_MAX:Number = 10;

		public function SearchCommand() {
			EventController.getInstance().addEventListener(ChangeModelEvent.KIZASI, changeModelHandler);
			EventController.getInstance().addEventListener(ChangeModelEvent.AMAZON, changeModelHandler);
			EventController.getInstance().addEventListener(ChangeModelEvent.YAHOO, changeModelHandler);
		}

		/**
		 * 実行メソッド
		 * 
		 */			
		public function execute(event:IEvent):void {

			// 検索キーワード
			var keyword:String = event.getData("keyword");
			ModelLocater.getInstance().words = keyword.split(" ");

			var kizaAPI:KizasiSearch = new KizasiSearch();

			kizaAPI.getSentense(keyword);
			
		}
	
		private var keywordSlide:SlideString;
		
		/**
		 * 検索処理のCompleteハンドラ
		 * 
		 */		
		private function changeModelHandler(event:ChangeModelEvent):void {
			
			var m:ModelLocater = ModelLocater.getInstance();
			
			if (event.type == ChangeModelEvent.AMAZON) {
				EventController.getInstance().dispatchEvent(new Event(Event.CHANGE));
			} else if (event.type == ChangeModelEvent.KIZASI) {
				var yahooAPI:YahooAPI = new YahooAPI();
				yahooAPI.MASearch(m.words.concat(m.kizasiWords.slice(0, KIZASI_RELATION_MAX)).join(" "));
			} else if (event.type == ChangeModelEvent.YAHOO) {
				var amazonAPI:AmazonBookSearch = new AmazonBookSearch();
				amazonAPI.searchBooks(m.words.concat(m.yahooWords));
			}	
		}
		
	}
}